/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mortbay.jetty.pico.dao.category;

import java.util.List;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.mortbay.jetty.pico.entities.Category;

/**
 *
 * @author godfather
 */
public class CategoryDAOImpl implements CategoryDAO{

    /*
     * behavior
     * 
     */
    
    public List<Category> getList() {
        return session.getNamedQuery("Category.findAll").list();
        //return session.createCriteria(Category.class).list();
    }

    
    public Category find(String id) {
        return (Category)session.createQuery("from Category where idCate = ?").setParameter(0, id).uniqueResult();
    }
    /*
     * property
     * 
     */
    /*
     * IOC
     */
    @Inject 
    private Session session;
    
    /*
     * Getter and Setter
     * 
     */
    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public void save(Category t) {
        this.session.persist(t);
    }

    public void remove(Category t) {
        int result = session.createQuery("delete from Category c where c.idCate = ?").setString(0, t.getIdCate()).executeUpdate();
        System.out.println("result: " + result);
        
    }

    public void removeByID(String o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Category> getParentCategories() {
        return session.createQuery("SELECT c FROM Category c WHERE c.cateState = 1 AND c.category IS NULL").list();
        
    }

    public List<Category> getCategoriesById(String id) {
        return session.createQuery("select c from Category c where c.category.idCate = ?").setString(0, id).list();
    }

    public Category getCategoryById(String id) {
        return (Category) session.getNamedQuery("Category.findByIdCate").setString("idCate", id).uniqueResult();
    }

}
